package com.lightMS.agenda.bl;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.lightMS.agenda.be.Event;
import com.lightMS.agenda.db.DatabaseConstants;

public class EventBL extends BusinessLogic{
	private static ArrayList<Event> events = new ArrayList<Event>();
	
	public static ArrayList<Event> getEvents(){
		Cursor eventsCursor = db.rawQuery(
				"SELECT " + 
					DatabaseConstants.EVENT_ID + ", " + 
					DatabaseConstants.EVENT_NAME + " " +
				"FROM " +
					DatabaseConstants.EVENT_TABLE +
				" ORDER BY " +
					DatabaseConstants.EVENT_ID,
			null);
		events.clear();
		if (eventsCursor.moveToFirst())
			do {
				long eventId= eventsCursor.getLong(DatabaseConstants.EVENT_ID_COLUMN);
				String eventName = eventsCursor.getString(DatabaseConstants.EVENT_NAME_COLUMN);
				Event event = new Event(eventId, eventName);
				events.add(event);
			} while(eventsCursor.moveToNext());
		return events;
	}
	
	public static void insertEvent(Event event){
		ContentValues cv = new ContentValues();
		cv.put(DatabaseConstants.EVENT_NAME, event.getName());
		db.insert(DatabaseConstants.EVENT_TABLE, null, cv);
		getEvents();
	}
	
	public static void deleteEvent(Event event) {
		String[] args={String.valueOf(event.getId())};
		db.delete(DatabaseConstants.EVENT_TABLE, DatabaseConstants.EVENT_ID + " = ?", args);
		getEvents();
	}
}
